package com.cloud.class04;

import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressTest {
    @Test
    public void test() throws IOException {
        InetAddress localHost = InetAddress.getLocalHost();
        String string = localHost.toString();
        int i = string.indexOf("/");
        System.out.println(string.substring(i + 1));
        System.out.println();
/*        byte[] address = InetAddress.getLocalHost().getAddress();
        StringBuilder stringBuilder = new StringBuilder();

        for (byte b : address) {
//            System.out.println(b);
            stringBuilder.append(b);
            stringBuilder.append(".");
        }
        String substring = stringBuilder.substring(0, stringBuilder.length() - 1);

        System.out.print(substring);*/


// ● public static InetAddress getByName(String host)
        InetAddress byName = InetAddress.getByName("www.baidu.com");
        System.out.println(byName);
//● public static InetAddress getByAddress(byte[] addr)
        InetAddress byAddress = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
        System.out.println(byAddress);
        System.out.println(byAddress.getHostName());


//        ● public String getHostAddress() ：返回 IP 地址字符串（以文本表现形式）
        System.out.println(byName.getHostAddress());
//● public String getHostName() ：获取此 IP 地址的主机名
        System.out.println(byName.getHostName());
//● public boolean isReachable(int timeout)：测试是否可以达到该地址
        boolean reachable = byName.isReachable(1000);
        System.out.println(reachable);
    }
}
